//
//  SEHospitalizeOrderDetailViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/17.
//

import Foundation
import UIKit

class SEHospitalizeOrderDetailViewModel: SEBaseViewModel {

    var model = SEInquiryInfo()

    var fromType: SEConfirmReserveViewControllerFromType = .hospitalizeReserve

    convenience init(model: SEInquiryInfo) {
        self.init()
        self.model = model
    }

    var isAgree = false

    var payViewModel: SEConfirmReservePayViewModel {
        return SEConfirmReservePayViewModel(price: String(format: "%.2lf", model.amount / 100.0), time: "", agree: isAgree)
    }

    var payHandler: ((SEHttpStatus) -> Void)?
    var commentHandler: ((SEHttpStatus) -> Void)?

    func numberOfRowsInSection(section: Int) -> Int {
        switch section {
        case 7: return model.status == .finish ? 3 : 2
        default: return 1
        }
    }

    func heightForRow(at indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0: return 80
        case 1: return 62
        case 3: return numberOfItemsForImgCv == 0 ? 0.01 : UITableView.automaticDimension
        case 5, 6: return 40
        case 7: return 50
        default: return UITableView.automaticDimension
        }
    }

    var numberOfItemsForImgCv: Int {
        return model.picList?.count ?? 0
    }

    var infoViewModel: SEInquiryInfoViewModel {
        return SEInquiryInfoViewModel(info: model)
    }

    var jobAndDeptDescribe: String {
        return "\(model.jobTitle ?? "")/\(model.deptName ?? "")"
    }

    var patientNameDescribe: String {
        return "就诊人: \(model.patientName ?? "")"
    }

    var patientSex: SEPatientSex {
        return SEPatientSex(rawValue: model.patientSex) ?? .unknown
    }

    var patientSexDescribe: String {
        return "性别: \(patientSex.title)"
    }

    var patientAgeDescribe: String {
        return "年龄: \(model.patientAge ?? "")岁"
    }

    var isHiddenDetail: Bool {
        switch model.status {
        case .waitPay, .unknown : return true
        default: return false
        }
    }

    var isHiddenPay: Bool {
        return model.status != .waitPay || model.status == .unknown
    }

    var isHiddenCancel: Bool {
        return isHiddenPay
    }

    var infoModelArray: [SEHospitalizeOrderInfoCellModel] {
        return [SEHospitalizeOrderInfoCellModel(titleText: "病情描述：",
                                                memoText: model.illness ?? "",
                                                titleColor: nil,
                                                titleFont: nil,
                                                memoColor: nil,
                                                memoFont: nil),

                SEHospitalizeOrderInfoCellModel(titleText: "医生建议：",
                                                memoText: model.proposal ?? "",
                                                titleColor: nil,
                                                titleFont: nil,
                                                memoColor: nil,
                                                memoFont: nil)]
    }

    var memoModelArray: [SEHospitalizeMemoCellModel] {
        return [SEHospitalizeMemoCellModel(titleText: "问诊时间：",
                                           memoText: model.createTime ?? ""),

                SEHospitalizeMemoCellModel(titleText: "订单编号：",
                                           memoText: model.orderId ?? "")]
    }

    var entranceModelArray: [SERegistrationConfirmPatientCellModel] {
        return [
                SERegistrationConfirmPatientCellModel(titleText: "问诊记录",
                                                      memoPlaceHolder: "点击查看",
                                                      memoText: "",
                                                      isHiddenArrow: false),
                SERegistrationConfirmPatientCellModel(titleText: "问诊处方",
                                                      memoPlaceHolder: model.isPrescription == 0 ? "未开方" : "查看处方",
                                                      memoText: "",
                                                      isHiddenArrow: model.isPrescription == 0)
            ]
}

    /// 问诊订单详情
    func requestHospitalizeOrderDetail() {
        SEHttpManager.shared.request(SEHospitalizeAPI.detailOfHospitalizeOrder(id: model.id ?? ""), model: SEInquiryInfo.self, withLoading: true) { (model, status) in
            switch status {
            case .success:
                self.model = model ?? SEInquiryInfo()
            default : break
            }
            self.updateData?(status)
        }
    }

    /// 订单支付
    func requestPayHospitalize() {
        guard isAgree else {
            SEHUD.showError(message: "请先阅读并同意患者须知")
            return
        }
        SEHttpManager.shared.requestForAny(SEPayAPI.pay(id: model.orderId ?? "", payType: 1, payChannel: 1), withLoading: true) { (result, status) in
            self.payHandler?(status)
        }
    }

    /// 问诊提交评价
    func submitCommentOfHospitalizeOrder() {
        SEHttpManager.shared.requestForAny(SEHospitalizeAPI.submitCommentOfHospitalizeOrder(objectId: model.id ?? "", rank: model.rank), withLoading: true) { result, status in
            self.commentHandler?(status)
        }
    }

}
